/**
 * Copyright (C) 2011  Robin Dietrich, Nils Kenneweg, Leonard Stutz
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 **/

#ifndef CHUNKMANAGER_HPP
#define	CHUNKMANAGER_HPP

#include <vector>
#include <boost/unordered_map.hpp>
#include <boost/unordered_set.hpp>
#include <boost/thread.hpp>

#include "tpl/singleton.hpp"
#include "config.hpp"
#include "chunk.hpp"

namespace minecraft {
    
    /** manager class for chunks which makes sure overall memory usage is limited */
    class chunkManager {
        private:
            /** list of currently chunks */
            boost::unordered_map<std::string, chunk*> chunks;
            /** vector containing chunk creating order */
            std::vector<std::string> chunks_created;
            
            /** threads for multithreaded chunk preloading */
            boost::unordered_set<boost::thread*> p_workers;
            /** mutex for chunk map synchronisation */
            boost::mutex p_lock;
            /** worker function for the preloading */
            void preloadWorker(int x, int y, int z);
            /** helps quiting coroutine stuff */
            bool coquit;
        public:
            /** constructor */
            chunkManager(); 
            /** destructor, frees all overlaying chunks */
            virtual ~chunkManager();
            
            /** add a chunk to the preload queue */
            boost::thread* preloadAdd(int x, int y, int z) {
                if (this->chunks.find(coordinatesToChunkId(x, y, z)) == this->chunks.end()) {
					boost::thread* thread = new boost::thread(boost::bind(&chunkManager::preloadWorker, this, x, y, z));
                    p_workers.insert(thread);
					
					return thread;
                }
				
				return NULL;
            }
            
            /** joins all preloading threads */
            void preloadJoin() {
                boost::unordered_set<boost::thread*>::const_iterator it;
                for (it = p_workers.begin(); it != p_workers.end(); ++it) {
                    (*it)->join();
                }
                
                p_workers.clear();
            }
            
            /** returns pointer to a chunk */
            chunk* getChunk(int x, int y, int z);
            /** returns type of block at specified position */
            b_type getBlock(int x, int y, int z);
            /** sets block at specified position */
            void setBlock(int x, int y, int z, b_type val);
            
            /** renders a chunk and returns it's mesh */
            Ogre::ManualObject* renderChunk(int x, int y, int z);
            
            /** returns current number of chunks loaded */
            unsigned int chunksLoaded() {
                return this->chunks_created.size();
            }
    };
    
    /** typedef for chunkManager accessed as a singleton */
    typedef Singleton<chunkManager> sChunkManager;
}

#endif	/* CHUNKMANAGER_HPP */

